# -*- coding:utf-8 -*-
"""
Python 3.7
Author：刘爽、何若溥
Time：2022/08/25
"""
import math
import os
from datetime import datetime
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd

from common.fileUtil import clearDir
from common.path import PICPATH
import numpy as np
from pandas.plotting import table
import matplotlib as mpl


def getChart(x_label, y_data, chart_name, x_name, y_name):
    matplotlib.rc("font", family='Microsoft YaHei')
    plt.xticks(range(len(y_data)), x_label, rotation=18)
    if y_data:
        y_max = max(y_data)
        y_kd = math.ceil(y_max/6)
    else:
        y_max = 1
        y_kd = 1
    plt.yticks(np.arange(0, y_max+1, y_kd))
    plt.xlabel(x_name)
    plt.ylabel(y_name)
    plt.title(chart_name)
    plt.tight_layout()
    plt.bar(range(len(y_data)), y_data)
    for x, y in zip(range(len(y_data)), y_data):
        plt.text(x, y, y, ha='center', va='bottom')

    plt.savefig(os.path.join(PICPATH, f"{chart_name}.png"))
    plt.close()
    # plt.show()


# 入参 pandas dataframe
def getExcelPic(bug_dataframe_data, bug_list_name):
    if bug_dataframe_data is None:
        bug_dataframe_data = pd.DataFrame([{"title": "未查到未解决问题，应均已解决，可到禅道核实！", "assignedTo": "无"}])
    mpl.rcParams['font.family'] = 'SimHei'
    fig, ax = plt.subplots(1, 3, figsize=(12, 3/15*len(bug_dataframe_data)+2))
    tb = ax[1].table(cellText=bug_dataframe_data.values, colLabels=bug_dataframe_data.columns, loc='center', cellLoc='left')
    # ax[1].set_title(f"{title_name}")
    [a.axis("off") for a in ax]
    tb.auto_set_font_size(False)
    tb.set_fontsize(8)
    tb.auto_set_column_width(col=list(range(len(bug_dataframe_data.columns))))  # Provide integer list of columns to adjust
    plt.savefig(os.path.join(PICPATH, f"{bug_list_name}.png"))
    # plt.show()



# if __name__ == '__main__':
#     label = ('active', 'resolved', 'closed')
#     data = (66, 9, 569)
#     label = ('gaojieqing', 'haoweige', 'liguangwei', 'liyanwei', 'zhangchengtao', 'zhenghaoyun')
#     data = (1, 7, 18, 26, 5, 6)
#     label = ('gaojieqing', 'haoweige')
#     data = [1, 100]
#     date = datetime.now().strftime('%Y%m%d%H%M%S')
#     title = f"Bug状态统计图_{date}"
#     yName = "Bug数量（个）"
#     xName = "Bug状态"
#     clearDir(PICPATH)
#     getChart(label, data, title, xName, yName)
